import axios from 'axios';
import qs from 'qs';
import md5 from 'js-md5';
import { ElMessage } from 'element-plus';

const customAxios = axios.create({
  baseURL: '/cedsApi',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
});

// 添加响应器
customAxios.interceptors.response.use(
  response => {
    return response;
      if (response.data.code === '2000') {
          return response;
      } else {
          // 使用 element-plus 的 ElMessage 返回警告，请求错误
          // ElMessage.warning(response.data.message || '暂无数据');
          ElMessage.warning('现场调查-地震震事件不存在' || '暂无数据');
          return Promise.reject(response);
      }
  },
  error => {
      // 使用 element-plus 的 ElMessage 返回报错
      ElMessage.error('报错信息：' + error.message);
      return Promise.reject(error);
  }
);

const MY = "da138a9602d37003980ecea673ef6468";

const createSign = (data)=>{
  var arr = [];
  for (const key in data) {
    if(data[key]){
      arr.push(key+"="+data[key])
    }
  }
var str = arr.sort().join("&");
return md5(str+MY);
}

// 查询当个地震
export const oneEarthQuake = (data) => {
  var data = {
    service: "getOneEvent",
    merchantNo: "syfzb001",
    eventCode: "1711074000376",
  }
  data.sign  = createSign(data)

  const formData = qs.stringify(data);
  return customAxios.post('', formData);
}

//批量获取数据
export const getBatchEvent = (time)=>{
  var data = {
    service: "getBatchEvent",
    merchantNo: "syfzb001",
    startTime: time,
  }
  data.sign  = createSign(data)

  const formData = qs.stringify(data);
  return customAxios.post('', formData);
}

//3.3获取调查数据接口

export const getCollectData = (eventCode)=>{
  var data = {
    service: "getCollectData",
    merchantNo: "syfzb001",
    eventCode: eventCode,
  }
  data.sign  = createSign(data)

  const formData = qs.stringify(data);
  return customAxios.post('', formData);
}

//3.3获取房屋调查数据接口

export const getCollectHouse = (eventCode)=>{
  var data = {
    service: "getCollectHouse",
    merchantNo: "syfzb001",
    eventCode: eventCode,
  }
  data.sign  = createSign(data)

  const formData = qs.stringify(data);
  return customAxios.post('', formData);
}

//3.3.6获取房屋单体调查数据接口
export const getCollectSingleHouse = (eventCode)=>{
  var data = {
    service: "getCollectSingleHouse",
    merchantNo: "syfzb001",
    eventCode: eventCode,
    collectId:""
  }
  data.sign  = createSign(data)

  const formData = qs.stringify(data);
  return customAxios.post('', formData);
}
//3.7获取生命线数据接口

export const getCollectLifelineNew = (eventCode)=>{
  var data = {
    service: "getCollectLifelineNew",
    merchantNo: "syfzb001",
    eventCode: eventCode,
    collectId:""
  }
  data.sign  = createSign(data)

  const formData = qs.stringify(data);
  return customAxios.post('', formData);
}

//3.5获取灾情急报明细接口
export const getCollectFast = (eventCode)=>{
  var data = {
    service: "getCollectFast",
    merchantNo: "syfzb001",
    eventCode: eventCode,
    collectId:""
  }
  data.sign  = createSign(data)

  const formData = qs.stringify(data);
  return customAxios.post('', formData);
}
//3.6获取地震灾害调查接口
export const getCollectDisaster = (eventCode)=>{
  var data = {
    service: "getCollectDisaster",
    merchantNo: "syfzb001",
    eventCode: eventCode,
    collectId:""
  }
  data.sign  = createSign(data)

  const formData = qs.stringify(data);
  return customAxios.post('', formData);
}

//3.7获取地震烈度圈接口
export const getIntensityCircle = (eventCode)=>{
  var data = {
    service: "getIntensityCircle",
    merchantNo: "syfzb001",
    eventCode: eventCode,
    fileType:2
  }
  data.sign  = createSign(data)

  const formData = qs.stringify(data);
  return customAxios.post('', formData,{ responseType: 'blob' });
}